package com.example.springboot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.example.springboot.Exception.ServiceException;
import com.example.springboot.common.Page;
import com.example.springboot.service.SystemNoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统公告表(SystemNotice)表服务实现类
 *
 * @since 2024-01-28 14:47:40
 */
@Service("systemNoticeService")
public class SystemNoticeServiceImpl implements SystemNoticeService {

    @Resource
    private SystemNoticeMapper systemNoticeMapper;


    /**
     *  新增系统公告
     * @param systemNotice
     */
    @Override
    public void addSystemNotice(SystemNotice systemNotice) {
        try {
            systemNoticeMapper
                    .addSystemNotice(systemNotice);
        }catch (Exception e){
            throw new ServiceException("新增系统公告失败");
        }
    }




    /**
     * 条件查询
     *
     * @param title
     * @param content
     * @param publisher
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<SystemNotice> selectByManyCondition(String title, String content, String publisher, String startTime, String endTime) {
        try{
            // 处理时间段
            if(StrUtil.isBlank(startTime)){
                startTime = "1000-01-01 00:00:00";
            }
            if(StrUtil.isBlank(endTime)) {
                startTime = "9999-12-31 23:59:59";
            }
            return systemNoticeMapper.selectByManyCondition( title, content, publisher, startTime, endTime);
        }catch (Exception e){
            throw new  ServiceException("获取系统公告失败");
        }
    }


    /**
     * 条件查询     分页
     *
     * @param currentPage
     * @param pageSize
     * @param title
     * @param content
     * @param publisher
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Page<SystemNotice> selectByManyConditionInPage(Integer currentPage, Integer pageSize, String title, String content, String publisher, String startTime, String endTime) {
        try{
            // 计算起始行
            Integer startRow = (currentPage - 1) * pageSize;

            // 处理时间段
            if(StrUtil.isBlank(startTime)){
                startTime = "1000-01-01 00:00:00";
            }
            if(StrUtil.isBlank(endTime)) {
                startTime = "9999-12-31 23:59:59";
            }

            // 查询总记录数
            int totalCount = systemNoticeMapper.selectByManyCondition(title, content, publisher, startTime, endTime).size();
            List<SystemNotice> systemNotices = systemNoticeMapper.selectByManyConditionInPage(startRow, pageSize, title, content, publisher, startTime, endTime);

            // 封装返回结果
            Page<SystemNotice> page = new Page<>();
            page.setList(systemNotices);
            page.setTotal(totalCount);
            return page;
        }catch (Exception e){
            throw new  ServiceException("获取系统公告失败");
        }
    }



    /**
     * 获取所有的系统公告信息
     * @return
     */
    @Override
    public List<SystemNotice> selectAll() {
        try {
            return systemNoticeMapper.selectAll();
        }catch (Exception e){
            throw new  ServiceException("获取系统公告失败");
        }
    }


    /**
     * 获取最新的五条系统公告信息
     * @return
     */
    @Override
    public List<SystemNotice> selectLatest() {
        try {
            return systemNoticeMapper.selectLatest();
        }catch (Exception e){
            throw new  ServiceException("获取系统公告失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        if (ids == null || ids.size() == 0){
            throw new ServiceException("请选中后点击删除选中");
        }
        try{
            for (Integer id : ids) {
                systemNoticeMapper.deleteById(id);
            }
        }catch (Exception e){
            throw new ServiceException("删除失败");
        }
    }


    /**
     * 分页查找
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<SystemNotice> selectByPage(Integer currentPage, Integer pageSize) {
        // 计算起始行
        Integer startRow = (currentPage - 1) * pageSize;

        // 查询总记录数
        int totalCount = systemNoticeMapper.selectAll().size();

        // 查询分页数据
        List<SystemNotice> SystemNotices = systemNoticeMapper.selectByPage(startRow, pageSize);

        // 封装返回结果
        Page<SystemNotice> page = new Page<>();
        page.setList(SystemNotices);
        page.setTotal(totalCount);
        return page;
    }


    /**
     * 根据ID删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        try {
            systemNoticeMapper.deleteById(id);
        } catch (Exception e) {
            throw new ServiceException("删除失败");
        }
    }


    /**
     * 根据ID更新信息
     * @param systemNotice
     */
    @Override
    public void updateById(SystemNotice systemNotice) {
        try{
            systemNoticeMapper.updateById(systemNotice);
        }catch (Exception e){
            throw new ServiceException("更新失败");
        }
    }


}
